function columnFormatter(value, row, index) {
    if (value >= 0)
        return formatSl2(value, ',', '.');
    else
        return '<span style="color:red">' + formatSl2(value, ',', '.') + '</span>';
}

var bgfkjh = {
    grid: $("#bgfkjh_datagrid"),
    gridOptions: {
        url: $.webPath() + '/jhgl/bgfkjh/bgfkjhList',
        toolbar: "#bgfkjh_toolbar",
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        showFooter: true,
        fit: true,
        fitColumns: true,
        columns: [
            [
                {field: 'checked', title: '选择', checkbox: true},
//                {field: 'ywks', title: '业务科室', align: 'center', width: 20},
                {field: 'gys', title: '供应商', align: 'center', width: 30},
                {field: 'qc', title: '期初余额', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'hbzf', title: '货币支付', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'yf5', title: '本月应付5', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'yf4', title: '本月应付4', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'yf1', title: '本月应付1', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'yfqt', title: '本月其他应付', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'sqwf', title: '上期未付', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'bqsf', title: '本期实付', align: 'right', width: 15, formatter: columnFormatter},
                {field: 'yf', title: '本期应付', align: 'right', width: 15, formatter: columnFormatter},
//                {field: 'cx', title: '541冲销', align: 'right', width: 20, formatter: columnFormatter},
                {field: 'qm', title: '期末余额', align: 'right', width: 15, formatter: columnFormatter}
            ]
        ],
        queryParams: {
            nf: $("#bgfkjh_toolbar input[name='nf']").val(),
            yf: $("#bgfkjh_toolbar input[name='yf']").val(),
            gsId: $("#bgfkjh_toolbar input[name='gsId']").val(),
            gysId: $("#bgfkjh_toolbar input[name='gysId']").val()
        }
    },
    nf: $("#bgfkjh_toolbar input[name='nf']"),
    yf: $("#bgfkjh_toolbar input[name='yf']"),
    gs: $("#bgfkjh_toolbar input[name='gsId']"),
    gys: $("#bgfkjh_toolbar input[name='gysId']"),
    win: null,
    init: function () {
        bgfkjh.grid.datagrid(bgfkjh.gridOptions);
        bgfkjh.nf.numberspinner({
            min: 2000,
            max: 2100,
            editable: true
        });
        bgfkjh.yf.numberspinner({
            min: 1,
            max: 12,
            editable: true
        });
        bgfkjh.gs.jygsjgcombotree();
        bgfkjh.gys.qyjgzcombobox();
    },
    search: function () {
        bgfkjh.grid.datagrid("reload", {
            nf: $("#bgfkjh_toolbar input[name='nf']").val(),
            yf: $("#bgfkjh_toolbar input[name='yf']").val(),
            gsId: $("#bgfkjh_toolbar input[name='gsId']").val(),
            //bmId: $("#bgfkjh_toolbar input[name='bmId']").val(),
            gysId: $("#bgfkjh_toolbar input[name='gysId']").val()
        });
    },
    showDialog: function (title, button, url) {
        bgfkjh.win = $.window({
            title: title,
            width: 870,
            height: 470,
            cache: false,
            iconCls: 'icon-new',
            url: url,
            modal: true,
            buttons: button
        });
    },
    export: function () {
        var gysid = $("#bgfkjh_toolbar input[name='gysId']").val();
        if (typeof(gysid) == "undefined")
            gysid = "";
        window.open($.webPath() + "/jhgl/bgfkjh/export?nf=" + $("#bgfkjh_toolbar input[name='nf']").val() +
            "&yf=" + $("#bgfkjh_toolbar input[name='yf']").val() +
            "&gsId=" + $("#bgfkjh_toolbar input[name='gsId']").val() +
            //"&bmId=" + $("#bgfkjh_toolbar input[name='bmId']").val() || '' +
            "&gysId=" + gysid);
    },
    saveHandler: function () {
    },
    saveSuccessHandler: function (data) {
    }
}

$(function () {

    bgfkjh.init();

    $("#bgfkjh_toolbar a[name='fk']").click(function () {
        var selectRecord = bgfkjh.grid.datagrid("getSelected");
        if (!selectRecord) {
            $.messager.alert("提示", '未选中任何行！', 'warning');
            return;
        }
        var button = []
        bgfkjh.showDialog("货币支付", button, $.webPath() + '/jhgl/bgfkjh/hbzf?gsId=' + selectRecord.gsid + '&gysId=' + selectRecord.gysid)
    });
    $("#bgfkjh_toolbar a[name='cx']").click(function () {
        var selectRecord = bgfkjh.grid.datagrid("getSelected");
        if (!selectRecord) {
            $.messager.alert("提示", '未选中任何行！', 'warning');
            return;
        }
        var button = []
        bgfkjh.showDialog("541冲销", button, $.webPath() + '/jhgl/bgfkjh/hbcx?gsId=' + selectRecord.gsid + '&gysId=' + selectRecord.gysid)
    });

    $("#bgfkjh_toolbar a[name='dc']").click(function () {
        bgfkjh.export();
    });
    $("#bgfkjh_toolbar input:button").click(function () {
        bgfkjh.search();
    });

    //新增按钮
    $("#bgfkj_fksq").click(function () {
        bgfkj_fksq('保供付款申请', 'add_fksh');
    });


});

//新增保供付款单
function bgfkj_fksq(title, action) {
    var selectRecord = bgfkjh.grid.datagrid("getSelected");
    if (!selectRecord) {
        $.messager.alert("提示", '未选中任何行！', 'warning');
        return;
    }
    var bgfkyf = (selectRecord.yf.toString()).replace(".", "_");
    var href = $.webPath() + "/jhgl/bgfkjh/bgfkjh_fksq?gysId=" + selectRecord.gysid + "&gsId=" + selectRecord.gsid + "&bgfkyf=" + bgfkyf;
    var actionUrl = $.webPath() + "/sfgl/fksh/add_bgfksh";
    bgfkj_fksq_showDialog(title, href, actionUrl, selectRecord);
}

//弹出新增或修改的窗口
function bgfkj_fksq_showDialog(title, href, actionUrl, selectRecord) {
    var win = $.window({
        title: title,
        width: 800,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_bgfk').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            $("[name='bgfk.zt']").val("ybc");
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            var d = eval("(" + data + ")");
                            $(win).mask('hide');
                            if (d.statusCode == 200) {
                                $(win).window('close');
                                //$('#bgfkjh_datagrid').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: d.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', d.message, 'error');
                        }
                    });
                }
            }
        ],
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        }
    });
}